package org.dylan.evernote;

import com.evernote.auth.EvernoteAuth;
import com.evernote.auth.EvernoteService;
import com.evernote.clients.ClientFactory;
import com.evernote.clients.NoteStoreClient;
import com.evernote.clients.UserStoreClient;


public class EDAMContext {

	private static final String SANDBOX_AUTH_TOKEN = "S=s1:U=88e4b:E=149478adcce:C=141efd9b0d1:P=1cd:A=en-devtoken:V=2:H=aa90a646ac3554064b5e91958c4118d2";

	private static final String EVERNOTE_AUTH_TOKEN = "S=s363:U=363381c:E=1494c7c55f5:C=141f4cb29f9:P=1cd:A=en-devtoken:V=2:H=7176bff7b6905a88ff6d66da7bdbd996";
	
	private static final String YINXIANG_AUTH_TOKEN = "S=s21:U=481976:E=1495190f791:C=141f9dfcb93:P=1cd:A=en-devtoken:V=2:H=aabb45c12069af6c18318843604b017f";
	
	private UserStoreClient userStore;

	private NoteStoreClient noteStore;

	public EDAMContext() {
		try {
//			EvernoteAuth evernoteAuth = new EvernoteAuth(EvernoteService.SANDBOX, SANDBOX_AUTH_TOKEN);
			EvernoteAuth evernoteAuth = new EvernoteAuth(EvernoteService.YINXIANG, YINXIANG_AUTH_TOKEN);
			ClientFactory factory = new ClientFactory(evernoteAuth);
			userStore = factory.createUserStoreClient();

			boolean versionOk = userStore.checkVersion("Dylan Chen", com.evernote.edam.userstore.Constants.EDAM_VERSION_MAJOR, com.evernote.edam.userstore.Constants.EDAM_VERSION_MINOR);
			if (!versionOk) {
				System.err.println("Incompatible Evernote client protocol version");
				System.exit(1);
			}

			// Set up the NoteStore client
			noteStore = factory.createNoteStoreClient();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	public UserStoreClient getUserStore() {
		return userStore;
	}

	public NoteStoreClient getNoteStore() {
		return noteStore;
	}
	
	 public static String bytesToHex(byte[] bytes) {
		    StringBuilder sb = new StringBuilder();
		    for (byte hashByte : bytes) {
		      int intVal = 0xff & hashByte;
		      if (intVal < 0x10) {
		        sb.append('0');
		      }
		      sb.append(Integer.toHexString(intVal));
		    }
		    return sb.toString();
		  }

}
